//
//  LeftAlignedLayoutViewController.m
//  TYUIKit_Example
//
//  Created by 谢航宇_WORK on 2022/3/16.
//  Copyright © 2022 谢航宇. All rights reserved.
//

#import "LeftAlignedLayoutViewController.h"
#import "TYLeftAlignedLayout.h"
#import "Masonry.h"
#import "UIView+TYLoading.h"
#import "DemoModel.h"
@interface LeftAlignedLayoutViewController () <UICollectionViewDelegate,UICollectionViewDataSource,TYLeftAlignedLayoutDatasource>
@property (nullable, nonatomic) UICollectionView *collectView;
@end

@implementation LeftAlignedLayoutViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor lightGrayColor];
    [self collectView];
    DemoModel * model = [[DemoModel alloc]initWithString:@"1"];
}


- (UICollectionView *)collectView {
    if (!_collectView) {
        __weak typeof(self)weakSelf = self;
        TYLeftAlignedLayout * flowLayout = [[TYLeftAlignedLayout alloc]init];
        flowLayout.dataSource = self;
        _collectView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flowLayout];
        _collectView.delegate = self;
        _collectView.backgroundColor = [UIColor clearColor];
        _collectView.dataSource = self;
        [_collectView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewCell"];
        
        [_collectView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"UICollectionElementKindSectionHeader"];
        
        [_collectView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"UICollectionElementKindSectionFooter"];
        
        [self.view addSubview:_collectView];
        [_collectView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.mas_equalTo(weakSelf.view);
            make.bottom.mas_equalTo(weakSelf.view);
        }];
    }
    return _collectView;
}

#pragma mark ---瀑布流代理方法
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]){
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"UICollectionElementKindSectionHeader" forIndexPath:indexPath];
        headerView.backgroundColor = [UIColor redColor];
        return headerView;
    }
    else {
        UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"UICollectionElementKindSectionFooter" forIndexPath:indexPath];
        footerView.backgroundColor = [UIColor blueColor];
        return footerView;
    }
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 5;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 10;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"UICollectionViewCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor orangeColor];
    
    return cell;
}
 /// 获取item高度，返回itemWidth和indexPath去获取
- (CGSize)leftAlignedLayout:(TYLeftAlignedLayout *)collectionViewLayout itemSizeAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(60 + 30 * (arc4random()%10), 30);
}
 
 ///设置列边距-横向间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(TYLeftAlignedLayout *)collectionViewLayout columnSpacingForSection:(NSInteger)section {
    return 10;
}

 ///设置行边距-竖向间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(TYLeftAlignedLayout*)collectionViewLayout rowSpacingForSection:(NSInteger)section {
    return 20;
}



 ///设置区头高度
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(TYLeftAlignedLayout *)collectionViewLayout referenceHeightForHeaderInSection:(NSInteger)section {
    return 30;
}
 ///设置区尾高度
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(TYLeftAlignedLayout *)collectionViewLayout referenceHeightForFooterInSection:(NSInteger)section {
    return 30;
}

 ///设置区边距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(TYLeftAlignedLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(10, 10, 20, 20);
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
